<?php

namespace App\Services;

use App\Models\Crm_Line_Apply;
use App\Models\Crm_Line_Package;
use App\Traits\External;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class LineService
{
    public function lineApplyStatusUpdate($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Line_Apply::find($params['id']);
            $res->status = $params['status'];

            if(!$res->save()){
                throw new \Exception("更新Crm_Line_Apply牵线申请状态失败" );
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function lineApplyList($params)
    {
        $result = Crm_Line_Apply::getFilter($params)->Nodel()->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $result = External::optionResult($result,3);
        $list['list'] = $result;
        return $list;
    }

    public function lineApplyDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Line_Apply::find($params['id']);
            $res->is_del = 'Y';

            if(!$res->save()){
                throw new \Exception("删除Crm_Line_Apply牵线申请失败" );
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function lineList($params)
    {
        $result = Crm_Line_Package::getFilter($params)->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function lineUpdate($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            if(!empty($params['id'])){
                $res = Crm_Line_Package::where('id',$params['id'])->update([
                    'name' => $params['name'],
                    'nums' => $params['nums'],
                    'price' => $params['price'],
                    'status' => $params['status'],
                    'desc' => $params['desc']
                ]);
                if(!$res){
                    throw new \Exception("更新Crm_Line_Package牵线套餐失败" );
                }
            }else{
                $res = Crm_Line_Package::create([
                    'agencyid' => $params['aid'],
                    'name' => $params['name'],
                    'nums' => $params['nums'],
                    'price' => $params['price'],
                    'status' => $params['status'],
                    'desc' => $params['desc']
                ]);
                if(!$res){
                    throw new \Exception("添加Crm_Line_Package牵线套餐失败" );
                }
            }

            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function lineStatusUpdate($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Line_Package::find($params['id']);
            $res->status = $params['status'];

            if(!$res->save()){
                throw new \Exception("更新Crm_Line_Package牵线套餐状态失败" );
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
